package com.guldan.jaina.manager;

import com.guldan.core.manager.BaseManager;
import com.guldan.core.search.Pagination;
import com.guldan.core.utils.DateUtil;
import com.guldan.jaina.dto.citizenfail.CitizenFailDTO;
import com.guldan.jaina.dto.citizenfail.CitizenFailSearchDTO;
import com.guldan.jaina.utils.CitizenInfoDomainConvertUtils;
import com.guldan.jaina.utils.UserUtils;
import com.guldan.mapper.db.entity.CitizenFail;
import com.guldan.mapper.db.entity.CitizenFailExample;
import com.guldan.mapper.db.mapper.CitizenFailMapperExt;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Silas.
 * Date: 2016/12/17
 * Time: 14:14
 */

@Service
public class CitizenFailManager extends BaseManager {

    @Autowired
    private CitizenFailMapperExt citizenFailMapperExt;

    public CitizenFailDTO get(Integer id) {
        return CitizenInfoDomainConvertUtils.convert(citizenFailMapperExt.selectByPrimaryKey(id));
    }

    //只有添加操作
    public CitizenFailDTO save(CitizenFailDTO dto) {

        CitizenFail entity = new CitizenFail();
        if (dto.getId() == null) {
            //entity.setId(entity.getId());
            entity.setBatchId(dto.getBatchId());
            entity.setName(dto.getName());
            entity.setNameZw(dto.getNameZw());
            entity.setIdentTypeName(dto.getIdentTypeName());
            entity.setIdentNumber(dto.getIdentNumber());
            entity.setGenderName(dto.getGenderName());
            entity.setNationName(dto.getNationName());
            entity.setDegreeName(dto.getDegreeName());
            entity.setAddress(dto.getAddress());
            entity.setAddressZw(dto.getAddressZw());
            entity.setTelephone(dto.getTelephone());
            entity.setAreaId(dto.getAreaId());
            entity.setAreaName(dto.getAreaName());
            if (UserUtils.getUser() != null) {
                entity.setCreatorAreaId(UserUtils.getUser().getAreaId());
                entity.setCreator(UserUtils.getUser().getUserAccount());
            }
            entity.setIsMaster(dto.getMaster());
            entity.setRelationName(dto.getRelationName());
            entity.setHouseholdRegisterNumber(dto.getHouseholdRegisterNumber());
            entity.setHouseholdNumber(dto.getHouseholdNumber());
            entity.setCitizenInfoTypeName(dto.getCitizenInfoTypeName());
            entity.setHouseHoldTypeName(dto.getHouseHoldTypeName());
            entity.setBankName(dto.getBankName());
            entity.setBankNumber(dto.getBankNumber());
            entity.setFailReason(dto.getFailReason());
            entity.setFileUrl(dto.getFileUrl());

            entity.setCreateTime(DateUtil.getCurrentTime());

            citizenFailMapperExt.insertSelective(entity);
        }

        return CitizenInfoDomainConvertUtils.convert(entity);
    }

    public Pagination<CitizenFailDTO> list(CitizenFailSearchDTO searchDTO) {

        Pagination<CitizenFailDTO> result = new Pagination<CitizenFailDTO>(searchDTO.getCurrentPage(), searchDTO.getPerPage());
        CitizenFailExample example = buildSearchExample(searchDTO);
        int total = citizenFailMapperExt.countByExample(example);
        result.setTotalCount(total);
        if (total > 0) {
            List<CitizenFailDTO> data = new ArrayList<CitizenFailDTO>();
            List<CitizenFail> list = citizenFailMapperExt.selectByExampleWithPaging(example, result.getOffset(), result.getPageSize());
            for (CitizenFail entity : list) {
                CitizenFailDTO dto = CitizenInfoDomainConvertUtils.convert(entity);

                data.add(dto);
            }
            result.setList(data);
        }
        return result;
    }


    //build查询Example
    private CitizenFailExample buildSearchExample(CitizenFailSearchDTO search) {
        CitizenFailExample example = new CitizenFailExample();
        CitizenFailExample.Criteria criteria = example.createCriteria();

        if (StringUtils.isNotBlank(search.getBatchId())) {
            criteria.andBatchIdEqualTo(search.getBatchId());
        }
        if (StringUtils.isNotBlank(search.getIdentNumber())) {
            criteria.andIdentNumberEqualTo(search.getIdentNumber());
        }
        if (StringUtils.isNotBlank(search.getName())) {
            criteria.andNameEqualTo(search.getName());
        }
        if (StringUtils.isNotBlank(search.getOrderByClause())) {
            example.setOrderByClause(search.getOrderByClause());
        } else {
            example.setOrderByClause("id asc");
        }
        return example;
    }


    //获取上传批次id，和时间，list
    public List<CitizenFailDTO> uploadBatchList() {
        CitizenFailExample example = new CitizenFailExample();
        CitizenFailExample.Criteria criteria = example.createCriteria();
        criteria.andCreatorAreaIdEqualTo(UserUtils.getUser().getAreaId());
        example.setOrderByClause("create_time desc");
        List<CitizenFailDTO> dtoList = new ArrayList<>();
        List<CitizenFail> entityList = citizenFailMapperExt.seletUploadBatchList(example);

        if (entityList != null && entityList.size() > 0) {
            for (CitizenFail entity : entityList) {
                dtoList.add(CitizenInfoDomainConvertUtils.convert(entity));
            }
        }
        return dtoList;
    }

}
